require "lfs"
-- package.loaded["constant"] = require "vscripts.constant"
function getpathes(rootpath, pathes)
    pathes = pathes or {}
    for entry in lfs.dir(rootpath) do
        if entry ~= '.' and entry ~= '..' then
            local path = rootpath .. '\\' .. entry
            local attr = lfs.attributes(path)
            assert(type(attr) == 'table')
            
            if attr.mode == 'directory' then
                getpathes(path, pathes)
            else
                table.insert(pathes, path)
            end
        end
    end
    return pathes
end

function loadkv(str)
	local stack = {}
	local key = {}
	local value = {}
	local root = {}
	local i = 1
	local str_len = string.len(str)
	local tkey = {}
	while true do
		if i > str_len then
			break
		end
		local char = string.sub(str,i,i)
		local len = #stack
		local bottom = stack[len]
		if char == '\"' then
			if len < 1 then
				table.insert(stack,i)
			else
				if string.sub(str,bottom,bottom)~='\"' then
					table.insert(stack,i)
				else
					table.remove(stack)
					if #key == #value then
						table.insert(key,string.sub(str,bottom+1,i-1))
					else
						table.insert(value,string.sub(str,bottom+1,i-1))
						root[key[#key]] = value[#value]
					end
				end
			end
			i = i + 1
		elseif char == '{' then
			table.insert(stack,i)
			table.insert(tkey,key[#key])
			table.remove(key)
			local _i = 0
			root[tkey[#tkey]],_i = loadkv(string.sub(str,i+1))
			i = i + _i + 1
		elseif char == "}" then
			if bottom and string.sub(str,bottom,bottom) == '{' then
				table.remove(stack)
			else
				break
			end
		elseif char == "/" and (not bottom or string.sub(str,bottom,bottom) ~= '\"') then
			for j = i+1,str_len do
				if string.sub(str,j,j) == '\n' then
					i = j + 1
					break
				end
			end
		else
			i = i + 1
		end

	end
	return root,i
end
local function addTo(a,b)
	for i,v in ipairs(b) do
		table.insert(a,v)
	end
end
local function getFileName(path)
	local re = path:reverse()
	local len = path:len()
	local a = re:find("\\")
	return path:sub(len-a+2,len)
end
local function getFileExp( name )
	local re = name:reverse()
	local len = name:len()
	local a = re:find("%.")
	return name:sub(len-a+2,len)
end
local files = getpathes(lfs.currentdir())
local panoramas = getpathes("../../../../content/dota_addons/dfw/panorama")
addTo(files,panoramas)
local locales = {}
local localeskey = {}
local modifiers = {}
local modifierskey = {}
local abilities = {}
local abilitieskey = {}
local items = {}
local itemskey = {}
local shops = {}
local aabs = {}
local comments = {}
local citys = {}
for i,v in ipairs(files) do
	local fn = getFileName(v)
	local file = io.open(v)
	local data = file:read("*a")
	local exp = getFileExp(fn)
	if exp == "xml" or exp=="js" or exp=="txt" or exp=="kv" or exp == "lua" then
		file:close()
		if fn~="handleNotice.lua" and fn ~= "addon_locale_template.txt" then
			print("doing "..fn.."...")
			for key in (data:gmatch("\"#([%w_]*)\"")) do
				if not localeskey[key] then
					localeskey[key] = true
					table.insert(locales,key)
				end
			end
			for key in (data:gmatch("text=\"%#([%w_]*)\"")) do
				if not localeskey[key] then
					localeskey[key] = true
					table.insert(locales,key)
				end
			end
			for key in (data:gmatch("showWarn%(%s*\"([%w_#]*)\"")) do
				key = key:gsub("#","")
				if not localeskey[key] then
					localeskey[key] = true
					table.insert(locales,key)
				end
			end
			for key in (data:gmatch("GameUI.onErrorMsg%s*%(%s*%{%s*text%s*:%s*\"([%w_#]*)\"%s*%}%)")) do
				key = key:gsub("#","")
				if not localeskey[key] then
					localeskey[key] = true
					table.insert(locales,key)
				end
			end
			for key in (data:gmatch("\"(modifier_[%w_]*)\"")) do
				if not modifierskey[key] then
					table.insert(modifiers,key)
					modifierskey[key] = true
				end
			end
			for key in (data:gmatch("\"Ability%d+\"[%s\t]*\"([%w_]*)\"")) do
				if not abilitieskey[key] then
					abilitieskey[key] = true
					table.insert(abilities,key)
				end
			end
		end
		if fn == "npc_items_custom_.txt" then
			for key in (data:gmatch("\"(item_[%w_]*)\"")) do
				if not itemskey[key] then
					itemskey[key] = true
					table.insert(items,key)
				end
			end
			
		end
		if fn == "npc_units_custom.txt" then
			local shopdata = data:find("shopunitsend")
			shopdata = data:sub(1,shopdata) .. "\n}"
			local units = loadkv(shopdata)
			for k,v in pairs(units.DOTAUnits) do
				table.insert(shops,k)
			end
		end
		if fn == "npc_abilities_custom.txt" then
			local units = loadkv(data)
			for k,v in pairs(units.DOTAAbilities) do
				table.insert(aabs,k)
			end
		end
		if fn == "block.lua" then
			for key in (data:gmatch("name = \"([%w_]*)\",")) do
				print(key)
				table.insert(citys,key)

			end
		end
	end
end

local wf = io.open("addon_locale_template.txt","w")
wf:write("//begin to write notice\n")
for k,v in ipairs(locales) do
	wf:write("\""..v.."\"\t\t\t\"\"\n")
end
wf:write("//end to write notice\n\n\n\n")

wf:write("//begin to write modifiers\n")
for k,v in ipairs(modifiers) do
	local str = "\"DOTA_Tooltip_" ..v.."\"\t\t\t\"\"\n"
	wf:write(str)
	local str2 = "\"DOTA_Tooltip_" .. v .. "_Description".."\"\t\t\t\"\"\n"
	wf:write(str2)
end
wf:write("//end to write modifiers\n\n\n\n")

wf:write("//begin to write hero abilities\n")
for k,v in ipairs(abilities) do
	local str = "\"DOTA_Tooltip_ability_" ..v.."\"\t\t\t\"\"\n"
	wf:write(str)
	local str2 = "\"DOTA_Tooltip_ability_" .. v .. "_Description".."\"\t\t\t\"\"\n"
	wf:write(str2)
end
wf:write("//end to write hero abilities\n\n\n\n")

wf:write("//begin to write items\n")
for k,v in ipairs(items) do
	local str = "\"DOTA_Tooltip_ability_" ..v.."\"\t\t\t\"\"\n"
	wf:write(str)
	local str2 = "\"DOTA_Tooltip_ability_" .. v .. "_Description".."\"\t\t\t\"\"\n"
	wf:write(str2)
end
wf:write("//end to write items\n\n\n\n")

wf:write("//begin to write shopunit\nvar unit_info = {\n")
for k,v in ipairs(shops) do
	local str = "\t\"" ..v.."\":true,\n"
	wf:write(str)
end
wf:write("}\n//end to write shopunit\n\n\n\n")

wf:write("//begin to write abilities\n")
table.sort(aabs)
for k,v in ipairs(aabs) do
	local str = "\"DOTA_Tooltip_ability_" ..v.."\"\t\t\t\"\"\n"
	wf:write(str)
	local str2 = "\"DOTA_Tooltip_ability_" .. v .. "_Description".."\"\t\t\t\"\"\n"
	wf:write(str2)
end
wf:write("//end to write abilities\n\n\n\n")
wf:write("//begin to write cities\n")
for k,v in ipairs(citys) do
	wf:write("\""..v.."\"\t\t\t\"\"\n")
end
wf:write("//end to write cities\n\n\n\n")

wf:flush()
wf:close()
print("completed!!!!!!!!!!!!!")

--[[
	处理modifier的自动命名
]]

